package TuiLayer;
import ControlLayer.MachineryCtr;
import ModelLayer.Machinery;
import ModelLayer.Unit;
import java.util.Scanner;

/**
 * Vestbjerg project
 * @GroupB2 (UCN - Computer Science DMAJ0914) 
 */
public class MachineryUI
{
    // instance variables
    private MachineryCtr machineryCtr;
    private Machinery foundMachinery;
    private Unit foundUnit;

    /**
     * Constructor for objects of class MachineryUI
     */
    public MachineryUI()
    {
        machineryCtr = new MachineryCtr();
    }

    public void createMachinery(String barcode, String name, String description, double price) 
    {
        machineryCtr.createMachinery(barcode, name, description, price);
    }

    public Machinery findMachinery(String barcode) 
    {
        return machineryCtr.findMachinery(barcode);
    }

    public void updateMachinery(Machinery machinery, String barcode, String name, String description, double price) 
    {
        machineryCtr.updateMachinery(machinery, barcode, name, description, price);
    }

    public void deleteMachinery(Machinery machinery) 
    {
        machineryCtr.deleteMachinery(machinery);
    }

    private int getChoice()
    {
        Scanner keyboard = new Scanner(System.in);
        int choice = keyboard.nextInt();

        return choice;
    }

    // Machinerys menu
    public void menuStart() 
    {
        boolean exit = false;
        String print = null;
        while(!exit) 
        {
            writeMachinerysMenu(print);
            int choice = getChoice();

            if(choice == 1) 
            {
                print = machineryAddStart();
            }
            else if(choice == 2)
            {
                print = browseMenuStart();
            }
            else if(choice == 0) 
            {
                exit = true;
            }

        }
    }

    public String browseMenuStart() 
    {
        

        boolean exit = false;
        String print = null;
        print = machineryFindStart();
        while(!exit) 
        {
            writeBrowseMachineryMenu(print);
            int choice = getChoice();

            if(choice == 1) 
            {
                unitMenuStart();
            }
            else if(choice == 2) 
            {
                print = machineryUpdateStart();
            }
            else if(choice == 3)
            {
                print = machineryRemoveStart();

            }
            else if(choice == 0) 
            {
                exit = true;
            }

        }
        return print;
    }
    // Write machinery menu
    public void writeMachinerysMenu(String print) 
    {
        System.out.println("\f *** Machinery ***");
        System.out.println(" (1)Add");
        System.out.println(" (2)Find / Edit");
        System.out.println(" (0)Back");
        if(print != null) 
        {
            System.out.println("\n " + print);

        }
        System.out.print("\n Make your choice: ");
    }

    public void writeBrowseMachineryMenu(String print) 
    {
        System.out.println("\f *** Machinery barcode: " + foundMachinery.getBarcode() + " ***");
        System.out.println(" (1)Manage units");
        System.out.println(" (2)Edit");
        System.out.println(" (3)Delete");
        System.out.println(" (0)Back");
        if(print != null) 
        {
            System.out.println("\n " + print);

        }
        System.out.print("\n Make your choice: ");
    }

    // Start "Add a machinery"
    public String machineryAddStart() 
    {
        String print = null;
        System.out.println("\nEnter barcode: ");
        Scanner sc = new Scanner(System.in);
        String barcode = sc.nextLine();

        System.out.println("Enter name: ");
        String name = sc.nextLine();

        System.out.println("Enter description: ");
        String description = sc.nextLine();

        System.out.println("Enter price: ");
        double price = sc.nextDouble();

        Machinery machinery = machineryCtr.findMachinery(barcode);
        if(machinery == null) 
        {
            createMachinery(barcode, name, description, price);

            print = "Machinery #" + barcode + " has been added to your machinery.";
        }
        else 
        {
            print = "ERROR: machinery already exists.";
        }

        return print;
    }

    // Start "Find a machinery"
    public String machineryFindStart() 
    {
        String print = null;
        System.out.print("\nEnter barcode: ");

        Scanner sc = new Scanner(System.in);
        String barcode = sc.next();

        foundMachinery = findMachinery(barcode);
        if(foundMachinery != null) 
        {
            print = " # Found Machinery: #" + "\n" 
            + "  ID: " + foundMachinery.getBarcode() + "\n"
            + "  Name: " + foundMachinery.getName() + "\n"
            + "  Description: " + foundMachinery.getDescription() + "\n"
            + "  Price: " + foundMachinery.getPrice() + "\n";
        }
        else 
        {
            print = "ERROR: Machinery not found.";
        }
        return print;
    }

    // Start "Update a machinery"
    public String machineryUpdateStart() 
    {
        String print = null;
        System.out.println("\nEnter new barcode: ");
        Scanner sc = new Scanner(System.in);
        String newBarcode = sc.nextLine();

        System.out.println("Enter new name: ");
        String newName = sc.nextLine();

        System.out.println("Enter new description: ");
        String newDescription = sc.nextLine();

        System.out.println("Enter new price: ");
        double newPrice = sc.nextDouble();

        updateMachinery(foundMachinery,newBarcode,newName,newDescription,newPrice);

        print = "Machinery #" + newBarcode + " has been updated to:\n" + getMachineryInfo(foundMachinery);

        return print;
    }

    private String getMachineryInfo(Machinery machinery) 
    {
        String string = "  Barcode: " + machinery.getBarcode() + "\n"
            + "  Name: " + machinery.getName() + "\n"
            + "  Description: " + machinery.getDescription() + "\n"
            + "  Price: " + machinery.getPrice() + "\n";
        return string;
    }

    // Start "Remove a machinery"
    public String machineryRemoveStart() 
    {
        String print = null;

        deleteMachinery(foundMachinery);
        print = "Machinery #" + foundMachinery.getBarcode() + " has been deleted.";

        return print;
    }

    public void unitMenuStart() 
    {

        boolean exit = false;
        String print = null;

        while(!exit) 
        {
            print = listUnits();
            writeUnitMenu(print);
            int choice = getChoice();

            if(choice == 1) 
            {
                print = unitsAddMenuStart();
            }
            else if(choice == 2) 
            {
                print = unitsBrowseMenuStart();
            }

            else if(choice == 0) 
            {
                exit = true;
            }
        }

    }

    private String listUnits() 
    {
        String print = "List of units:\n";
        int counter = 1;
        if(foundMachinery.getUnitList().size() > 0) {
            for(Unit unit : foundMachinery.getUnitList()) 
            {
                String availibility = null;
                if(unit.isAvailible()) 
                {
                    availibility = "availible";
                }
                else 
                {
                    availibility = "not availible";
                }
                print += " " + counter + ":   " + unit.getSerialNumber() + " - " + availibility + "\n";
                counter++;
            }
        }
        else 
        {
            print = "There are no units of this machinery.";
        }
        return print;
    }

    public void writeUnitMenu(String print) 
    {
        System.out.println("\f *** Units of machinery #" + foundMachinery.getBarcode() + " ***");
        System.out.println(" (1)Add");
        System.out.println(" (2)Find / Edit");
        System.out.println(" (0)Back");
        if(print != null) 
        {
            System.out.println("\n " + print);

        }
        System.out.print("\n Make your choice: ");
    }

    public String unitsAddMenuStart() 
    {
        String print = null;
        System.out.println("\nEnter serial number: ");
        Scanner sc = new Scanner(System.in);
        String serialNumber = sc.next();

        Unit unit = foundMachinery.findUnit(serialNumber);
        if(unit == null) 
        {
            machineryCtr.createUnit(foundMachinery,serialNumber);
            print = "Unit #" + serialNumber + " created.";
        }
        else 
        {
            print = "ERROR: Unit already exists.";
        }

        return print;
    }

    public String unitsBrowseMenuStart() 
    {
        

        boolean exit = false;
        String print = null;
        print = unitFindStart();
        while(!exit) 
        {
            writeBrowseUnitsMenu(print);
            int choice = getChoice();

            if(choice == 1) 
            {
                print = unitUpdateStart();
            }
            else if(choice == 2) 
            {
                print = unitRemoveStart();
            }
            else if(choice == 3) 
            {
                unitAvailibility();
            }
            else if(choice == 0) 
            {
                exit = true;
            }

        }
        return print;
    }
    // Write machinery menu
    public void writeBrowseUnitsMenu(String print) 
    {
        System.out.println("\f *** Unit #" + foundUnit.getSerialNumber() + " ***");
        System.out.println(" (1)Update serial number");
        System.out.println(" (2)Remove");
        if(foundUnit.isAvailible()) 
        {
            System.out.println(" (3)Mark as not availible");
        }
        else 
        {
            System.out.println(" (3)Mark as availible");
        }
        System.out.println(" (0)Back");
        if(print != null) 
        {
            System.out.println("\n " + print);

        }
        System.out.print("\n Make your choice: ");
    }

    public String unitFindStart() 
    {
        String print = null;
        System.out.print("\nEnter serial number: ");

        Scanner sc = new Scanner(System.in);
        String serialNumber = sc.next();

        foundUnit = foundMachinery.findUnit(serialNumber);
        if(foundMachinery != null) 
        {
            print = " Found unit:" + "\n" 
            + "  ID: " + foundUnit.getSerialNumber() + "\n";
        }
        else 
        {
            print = "ERROR: Unit not found.";
        }
        return print;
    }

    // Start "Update a machinery"
    public String unitUpdateStart() 
    {
        String print = null;
        System.out.println("\nEnter new serial number: ");
        Scanner sc = new Scanner(System.in);
        String newSerialNumber = sc.next();

        machineryCtr.updateUnit(foundUnit,newSerialNumber);

        print = "Unit ID has been updated to #" + newSerialNumber + " .";

        return print;
    }

    // Start "Remove a machinery"
    public String unitRemoveStart() 
    {
        String print = null;

        machineryCtr.deleteUnit(foundMachinery,foundUnit);
        print = "Unit #" + foundUnit.getSerialNumber() + " has been deleted.";

        return print;
    }

    public void unitAvailibility() 
    {
        if(foundUnit.isAvailible()) 
        {
            foundUnit.setAvailibility(false);
        }
        else 
        {
            foundUnit.setAvailibility(true);
        }
    }
}

